<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>import</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<link href="/resources/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
    <script src="/resources/js/jQuery.js" type="text/javascript"></script>
    <script src="/resources/js/ajaxupload.js" type="text/javascript"></script>
    <script src="/resources/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>

</head>

<body>

	 <form id="excelAddForm" method="post" encType="multipart/form-data" >
          <div >
 			  <input type="file" name="excelFile" id="excelFile" onchange="picChange('excelFile')"/>
 			  <button onclick="ajaxFileUpload('excelFile')">上传</button>
         </div>  
     </form> 
	
<!-- 	<form id="excelAddForm" method="post" encType="multipart/form-data">
		<table align="center">
			<tr>
				<td>文件：</td>
				<td><input type="file" name="excelFile" id="excelFile" onchange="picChange('excelFile')"/>
   			    (只能上传xls、xlsx格式的文件)</td>
			</tr>
			<tr>
				<td colspan="2">
					<div style="padding:5px;text-align:center;">
				<a href="javascript:void(0);"onclick="ajaxFileUpload('excelFile')">上传</a>
			</div>
				</td>
			</tr>
		</table>
	</form> -->
	<script type="text/javascript">
	 var dialog = frameElement.dialog; //调用页面的dialog对象(ligerui对象)
	 var url = dialog.options.data.url;
	function ajaxFileUpload(id){
		var filepath = $("#"+id).val();
		if(!filepath){
			$.ligerDialog.warning('文件格式错误！');
			return false;
		}
	    //开始上传文件时显示一个图片,文件上传完成将图片隐藏
	    //$("#loading").ajaxStart(function(){$(this).show();}).ajaxComplete(function(){$(this).hide();});
	    //执行上传文件操作的函数
	    $.ajaxFileUpload({
	        //处理文件上传操作的服务器端地址(可以传参数,已亲测可用)
	        url:url,
	        secureuri:false,                       //是否启用安全提交,默认为false 
	        fileElementId:id,           //文件选择框的id属性
	        dataType:'json',                       //服务器返回的格式,可以是json或xml等
	        success:function(data, status){        //服务器响应成功时的处理函数
	            data = data.replace("<PRE>", '');  //ajaxFileUpload会对服务器响应回来的text内容加上<pre>text</pre>前后缀
	            data = data.replace("</PRE>", '');
	            data = data.replace("<pre>", '');
	            data = data.replace("</pre>", ''); //本例中设定上传文件完毕后,服务端会返回给前台[0`filepath]
	            if(data.substring(0, 1) == 0){     //0表示上传成功(后跟上传后的文件路径),1表示失败(后跟失败描述)
	                $("img[id='uploadImage']").attr("src", data.substring(2));
	                $('#result').html("图片上传成功<br/>");
	                
	            }else{
	                $.ligerDialog.error('图片上传失败，请重试！！');
	            }
	        },
	        error:function(data, status, e){ //服务器响应失败时的处理函数
	        	 $.ligerDialog.error('图片上传失败，请重试！！');
	        }
	    });
	}

	
		function picChange(id){
			var filepath = $("#"+id).val();
			filepath=filepath.substring(filepath.lastIndexOf('.')+1,filepath.length);
			filepath=filepath.toLowerCase();
			if(filepath != 'xls' && filepath != 'xlsx'){
				var file = document.getElementById(id);//清空file input框 
				if (file.outerHTML) {
		             file.outerHTML = file.outerHTML;
		         } else { 
		             file.value = "";
		         }
				$.ligerDialog.warning('文件格式错误！', function (type)
					 {
					return false;
					});
				$("#"+id).val('');
				}
			}
	</script>
</body>
</html>
